﻿using ETWJ.DTO;
using ETWJ.Iservice;
using ETWJ.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace ETWJ.Service
{
    public class OrderService : CommonService<Order, OrderDTO, MyDbContext>, IOrderService<Order, OrderDTO>
    {
        public async Task<List<OrderCompanyListModel>> GetOrderByPage(int pageIndex = 1, int count = 5)

        {
            using (MyDbContext ctx = new MyDbContext())
            {
                var data =await ctx.Set<Order>().Where(e => e.IsDeleted == 0)
                  .Join(ctx.Set<Company>().Where(e=>e.IsDeleted==0), a => a.CompanyId, g => g.Id, (a, g) => new OrderCompanyListModel
                  {
                      CompanyId = a.CompanyId,
                      CompanyName = g.CompanyName,
                      OrderCode = a.OrderCode,
                      OrderId = a.Id,
                      AddTime = a.AddTime,
                      OrderTime = a.OrderTime,
                      DeliveTime = a.DeliveTime,
                      SinglePeople = a.SinglePeople,
                      OrderSumMoney = a.OrderSumMoney,
                      NeedCost = a.NeedCost
                  }).Skip((pageIndex-1)*count).Take(count).ToListAsync();

                return data;

                #region 使用Linq进行左连接查询
                //var companys = ctx.Set<Company>().Where(e => e.IsDeleted == 0).AsNoTracking().ToList();
                //var orders = ctx.Set<Order>().Where(e => e.IsDeleted == 0).AsNoTracking().ToList();
                //var companyOrders = from o in orders
                //                    join c in companys
                //                    on o.CompanyId equals c.Id into result
                //                    from data in result.DefaultIfEmpty()
                //                    select new OrderCompanyListModel()
                //                    {
                //                        CompanyId = o.CompanyId,
                //                        CompanyName = data.CompanyName,
                //                        OrderCode = o.OrderCode,
                //                        OrderSumMoney = o.OrderSumMoney,
                //                        OrderId = o.Id,
                //                        OrderTime = o.OrderTime,
                //                        DeliveTime = o.DeliveTime,
                //                        NeedCost = o.NeedCost,
                //                        AddTime = o.AddTime

                //                    };
                //return companyOrders.ToList();
                #endregion
            }
        }

        public async Task<int> GetOrderCountAsync()
        {
            using (MyDbContext ctx = new MyDbContext())
            {
                var data = await ctx.Set<Order>().Where(e => e.IsDeleted == 0)
                  .Join(ctx.Set<Company>(), a => a.CompanyId, g => g.Id, (a, g) => new OrderCompanyListModel
                  {
                      CompanyId = a.CompanyId,
                      CompanyName = g.CompanyName,
                      OrderCode = a.OrderCode,
                      OrderId = a.Id,
                      AddTime = a.AddTime,
                      OrderTime = a.OrderTime,
                      DeliveTime = a.DeliveTime,
                      SinglePeople = a.SinglePeople,
                      OrderSumMoney = a.OrderSumMoney,
                      NeedCost = a.NeedCost
                  }).AsNoTracking().ToListAsync();

                return data.Count();
               
            }
        }

        public async Task<int> Update(OrderDTO dto)
        {
            using (MyDbContext ctx = new MyDbContext())
            {
                BaseService<Order> bs = new BaseService<Order>(ctx);
                var model =await bs.GetByIdAsync(dto.Id);
                model.CompanyId = dto.CompanyId;
                model.DeliveTime = dto.DeliveTime;
                model.OrderCode = dto.OrderCode;
                model.OrderSumMoney = dto.OrderSumMoney;
                model.OrderTime = dto.OrderTime;
                model.SinglePeople = dto.SinglePeople;

                return await ctx.SaveChangesAsync();

            }
        }
    }
}
